<?php
class DiplomaIndexController  extends Zone_Action{
	public function Fields($default=NULL){
		$data = array(
			type => array(
				type 		=> 'CHAR',
				form_label => 'Loại',			
				default_value =>"get"
			),
			subtype => array(
				type 		=> 'INT',
				form_label => 'Loại công văn',
				default_value =>null		
			),
			date_enactment => array(
				type 		=> 'DATE',
				no_empty =>true,
				fix_value	=> change_date_format,
				form_label => 'Ngày phát hành',
				default_value =>null
			),
			date_get => array(
				type => 'DATE',
				no_empty =>true,
				fix_value	=> change_date_format, 
				form_label => 'Ngày đến',
				default_value =>null
			),
			code=>array(
				type		=>'TEXT',
				no_empty	=>true,
				form_label	=>'Số hiệu công văn',
				default_value =>null
			),
			ogran_by =>array(
				type		=>"TEXT",
				no_empty	=>true,
				form_label	=>"Cơ quan ban hành",
				default_value =>null
			),
			abstracts  => array(
				type 			=> 'TEXT',
				no_empty		=>true,
				form_label 		=>'Trích yếu',
				default_value =>null
			),
			prior	=> array(
				type 			=> 'TEXT',
				form_label 		=> 'Mức độ ưu tiên xử lý',
				no_empty		=>true,
				default_value =>null
			),
			desc	=> array(
				type 			=> 'TEXT',
				form_label 		=> 'Ghi chú',
				default_value =>null
			),
			
			date_updated =>array(
				type			=>'DATETIME',
				form_label		=>'Ngày cập nhật',
				default_value	=>mysql_date(),
			),
			updated_by_id =>array(
				type			=>'INT',
				form_label		=>'Người chỉnh sửa',
				default_value	=>get_user_id()
			)
		);
		/* Neu chinh sua thi cac thong so duoc mac dinh trc */
		if( is_array( $default ) ){
			foreach( $data as $k => $config ){
				if( $config['type'] != 'NONE' && isset( $default[$k] ) ){
					$data[$k]['default_value'] = $default[$k];
				}
			}
		}	
		return $data;	
	}
	
	public function CommentFields($default=NULL){
		$data = array(
			content => array(
				type 		=> 'HTML',
				no_empty 	=>true,
				form_label 	=> 'Ý kiến phê duyệt',			
			),
			date_created		=>array(
				type 			=>'DATETIME',
				default_value 	=>mysql_date()
			),
			created_by_id		=>array(
				type 			=>'INT',
				default_value 	=>get_user_id()
			)
		);
		/* Neu chinh sua thi cac thong so duoc mac dinh trc */
		if( is_array( $default ) ){
			foreach( $data as $k => $config ){
				if( $config['type'] != 'NONE' && isset( $default[$k] ) ){
					$data[$k]['default_value'] = $default[$k];
				}
			}
		}	
		return $data;	
	}
	
	public function initAction(){
		//if(!$this->hasRole("diploma")){
		//	return $this->setContent(error("Bạn không có quyền truy cập công văn"));
		//}
		$this->set(array(
			type =>$this->Model->fetchAll("SELECT * FROM `diplomas_types`")
		));
	}
	

	public function indexAction(){
		if(!$this->hasRole("add_diploma")){
			return $this->setContent(error("Bạn không có quyền truy cập phân hệ công văn."));
		}
		$type	 = get("bl");
		
		$user_id = get_user_id();
		$vars = array();
		$condition = "";
		switch ($type) {
			case 'indate':
				$condition = " AND DAY(`a`.`date_get`)=DAY(NOW())";
				break;
			case 'inmonth':
				$condition = " AND MONTH(`a`.`date_get`)=MONTH(NOW())";
				break;
		}
		
		$WHERE = array();
		$order_by = " ORDER BY `a`.`ID` DESC";
		
		$current_page = max(getInt('p',1),1);		
		$range_page = max(10,(int)$this->getConfig('user.page',20));
		
		if( isset($_REQUEST['p'])){
			$vars['p'] = $current_page;
		}
		
		$limit = "LIMIT ".($current_page-1)*$range_page.",$range_page";
				
		$field_order = array('subtype','code','date_get','title','ogran_by','abstracts');
		foreach( $field_order as $f ){
			if( get('order_by') == $f ){
				$order_by = "ORDER BY $f ". ( get('order_type') == 'desc' ? 'desc' : 'asc');
				$vars['order_by'] = $f;
				if( get('order_type') == 'desc' ){
					$vars['order_by']='desc';
				}
				break;	
			}	
		}
			
		$total = $this->Model->fetchOne("
			SELECT COUNT(*) 
			FROM `diplomas` as `a`
				LEFT JOIN `diplomas_users` as `b` 
					ON `b`.`diploma_id`=`a`.`ID` AND `b`.`user_id`='$user_id'
			WHERE `a`.`is_draft`='no' 
				AND `a`.`type`='get'  
				AND ( `b`.`is_decide`='yes' OR ( `a`.`decided`='yes' AND `b`.`is_proc`='yes' ) OR `a`.`created_by_id`='$user_id' )
				$condition
			$order_by $limit
		");
		$posts = $this->Model->fetchAll("
			SELECT `a`.*,`b`.`title` as `subtype_title`
				FROM `diplomas` as `a` 
					LEFT JOIN `diplomas_types` as `b` 
						ON `a`.`subtype`=`b`.`ID`
					LEFT JOIN `diplomas_users` as `c` 
						ON `c`.`diploma_id`=`a`.`ID` AND `c`.`user_id`='$user_id'
				WHERE `a`.`type`='get' 
					AND `a`.`is_draft`='no'
					AND ( `c`.`is_decide`='yes'  OR ( `a`.`decided`='yes' AND `c`.`is_proc`='yes' ) OR `a`.`created_by_id`='$user_id'  )
					$condition
			$order_by $limit	
		");
		
		$a = $vars;
		unset($a['p']);
		
		$page = page_ajax('#Diploma?'.to_query_configs($a).'&p=', $total , $range_page, $current_page);	
			
		$this->set(array(
			user_id	=> get_user_id(),
			page	=> $page,
			total	=> $total,
			vars	=> $vars,
			post	=> $posts
		));
	}

	
	public function uploadAction(){
		$this->removeLayout();
		$diploma_id = getInt("ID",0);
			
		$user_id = get_user_id();
		if( isPost()){
			loadClass("ZData");
			$f = new ZData();
			$f->addField(array(
				file => array(
					type 		=> 'FILE',
					path		=> 'files/diploma/',
					no_empty	=> true
				)
			));
			
			$data = $f->getData();
			if( is_array( $data ) ){
				$file = $data['file'];
				
				$this->Model->insert("diplomas_files",array(
					diploma_id		=> $diploma_id,
					name			=> $file[0],
					type			=> $file[1],
					size			=> $file[2],
					filename		=> $file[3]
				));
				
				$file_id = $this->Model->lastId();				
				return $this->setJSON(array(
					content	=> "<div class='x-file-info'><input checked type='checkbox' name='files[]' value='$file_id'/>
								<a href='".ctrUrl($this)."/File?ID=$file_id' target='_blank'>{$file[3]} {$file[2]} KB	</a>
								</div>"
				));
			}else{
				return $this->setJSON(array(
					alert => error($data)
				));				
			}
			
		}	
	}
	
	public function addAction(){
		if(!$this->hasRole("add_diploma")){
			return $this->setContent(error("Bạn không có quyền tạo công văn"));
		}
		$user_id = get_user_id();
		
		$post = $this->Model->fetchRow("SELECT * FROM `diplomas` WHERE `created_by_id`='$user_id' AND `is_draft`='yes' AND `type`='get' LIMIT 1");
		
		if($post){
			$diploma_id = $post['ID'];
			$files = $this->Model->fetchAll("SELECT * FROM `diplomas_files` WHERE `diploma_id`='$diploma_id'");
		}else{
			$this->Model->insert("diplomas",array(
				is_draft		=> 'yes',
				type			=> 'get',
				created_by_id	=> $user_id			
			));
			$post 	 = array();
			$files   = array();
			$diploma_id = $this->Model->lastId();
		}
		
		$this->set(array(
			post				=> 	$post,
			diploma_id 			=>	$diploma_id,
			files				=> 	$files,
			users				=>	$this->Model->fetchRow("SELECT * FROM `users` WHERE `ID`='$user_id'")
		));
		if(isPost()){
			
			$diploma_id = $_POST['diploma_id'];
			loadClass("ZData");
			$form = new ZData();
			$form->addFields( $this->Fields($post));
			$data = $form->getData();
			
			if( is_array($data)){
				
				if($post["date_created"]==null){
					$data['created_by_id'] = get_user_id();
					$data['date_created']  = date("Y-m-d H:i:s");
				}
				
				$date_get 		= $data['date_get'];
				$date_enactment = $data['date_enactment'];
				
				
				$chekdate = $this->Model->fetchOne("SELECT DATEDIFF('$date_enactment','$date_get')");
				
				if($chekdate>0){
					return $this->setJSON(array(
						message => error("Ngày ban hành và ngày đến không hợp lệ")
					));
				}
				$data['is_draft']='no';
				$code = $data['code'];
				if(count($this->Model->fetchAll("SELECT * FROM `diplomas` WHERE `code`='$code'"))>0){
					return $this->setJSON(array(
						message => error("Số hiệu công văn không được trùng")
					));
				}
				////////////////////////////////
				$roles = array();				
				$usersProc = (array)$_POST['usersProc'];				
				foreach( $usersProc as $k => $uid ){
					if( !$this->Model->fetchRow("SELECT * FROM `users` WHERE `ID`='$uid' AND `is_deleted`='no'") ){
						continue;
					}	
					$roles[$uid] = array(
						diploma_id	 => $diploma_id,
						user_id		 => $uid,
						is_proc => 'yes'
					);	
				}	
				$usersDecide = (array)$_POST['usersDecide'];
				if(count($usersDecide)==0){
					return $this->setJSON(array(
						message	=> error("Bạn chưa chọn người phê duyệt công văn")
					));
				}
				
				
				foreach( $usersDecide as $k => $uid ){
					if( !$this->Model->fetchRow("SELECT * FROM `users` WHERE `ID`='$uid' AND `is_deleted`='no'") ){
						continue;
					}
					if( isset($roles[$uid])){
						$roles[$uid]['is_proc'] = 'yes';
						$roles[$uid]['is_decide'] = 'yes';
					}else{
						$roles[$uid] = array(
							diploma_id	=> $diploma_id,
							user_id		=> $uid,
							is_decide	=> 'yes'
						);
					}
					$this->Model->insert('notices',array(
						created_by_id	=> $user_id,
						user_id			=> $uid,
						url				=> "#Diploma/Index/View?ID=$diploma_id",
						title			=> "Bạn vừa nhận được một công văn mới",
						date			=> mysql_date(),
						content			=> "Bạn vừa nhận được một công văn mới."
					));
				}
				$this->Model->update("diplomas",$data," `ID`='$diploma_id'");
				foreach( $roles as $role ){
					$this->Model->insert('diplomas_users',$role);
				}
				
				///////////////////////////////
				$files_selected = (array)$_POST['files'];
				$files = $this->Model->fetchAll("SELECT * FROM `diplomas_files` WHERE `diploma_id`='$diploma_id'");
				foreach( $files as $f ){
					if( !in_array( $f['ID'], $files_selected ) ){
						$this->Model->delete("diplomas_files","`ID`='{$f['ID']}'");
						@unlink("files/diploma/{$f['name']}");
					}
				}
				
				return $this->setJSON(array(
					redirect	=> "#Diploma"
				));
			}else{
				return $this->setJSON(array(
					message => error($data)
				));
			}			
		}
	}
	
	
	public function editAction(){
		$user_id = get_user_id();
		$diploma_id = getInt('ID',0);
		$post = $this->Model->fetchRow("
			SELECT `a`.* FROM `diplomas` as `a` 
				WHERE `a`.`ID`='$diploma_id'");
		
		
		
		$decide=array();
		$proc=array();
		
		$files = $this->Model->fetchAll("SELECT * FROM `diplomas_files` where `diploma_id`='$diploma_id'");

		$userProc = $this->Model->fetchAll("
			SELECT `a`.*,`b`.*
			FROM `diplomas_users` as `a` 
				LEFT JOIN `users` as `b` ON `b`.`ID`=`a`.`user_id`
			WHERE `a`.`diploma_id`='$diploma_id' AND `a`.`is_proc`='yes'
		");
		$userDec = $this->Model->fetchAll("
			SELECT `a`.*,`b`.*
			FROM `diplomas_users` as `a` 
				LEFT JOIN `users` as `b` ON `b`.`ID`=`a`.`user_id`
			WHERE `a`.`diploma_id`='$diploma_id' AND `a`.`is_decide`='yes'
		");
		$this->set(array(
			post 				=> 	$post,
			diploma 			=>	$diploma_id,
			files				=> 	$files,
			users				=> 	$this->Model->fetchRow("SELECT * FROM `users` WHERE `ID`='$user_id'"),
			usersDecide			=> 	$userDec,
			usersProc			=> 	$userProc
		));
		if(isPost()){
			loadClass("ZData");
			$diploma_id = $_POST["diploma_id"];
			
			$form = new ZData();
			$form->addFields( $this->Fields($post));
			$data = $form->getData();
			
			if( is_array($data)){
				if($post["date_created"]==null){
					$data['created_by_id'] = get_user_id();
					$data['date_created']  = date("Y-m-d H:i:s");
				}
				$date_get 		= $data['date_get'];
				$date_enactment = $data['date_enactment'];
				
				$chekdate = $this->Model->fetchOne("SELECT DATEDIFF('$date_enactment','$date_get')");
				if($chekdate>0){
					return $this->setJSON(array(
						message => error("Ngày ban hành và ngày đến không hợp lệ")
					));
				}
				$data['is_draft']='no';
				$this->Model->update("diplomas",$data,"`ID`='$diploma_id'");
				
				////////////////////////////////
				$this->Model->delete("diplomas_users","`diploma_id`='$diploma_id'");
				////////////////////////////////
				$roles = array();				
				$usersProc = (array)$_POST['usersProc'];				
				foreach( $usersProc as $k => $uid ){
					if( !$this->Model->fetchRow("SELECT * FROM `users` WHERE `ID`='$uid' AND `is_deleted`='no'") ){
						continue;
					}	
					$roles[$uid] = array(
						diploma_id	 => $diploma_id,
						user_id		 => $uid,
						is_proc => 'yes'
					);	
				}	
				$usersDecide = (array)$_POST['usersDecide'];
				if(count($usersDecide)==0){
					return $this->setJSON(array(
						message	=> error("Bạn chưa chọn người phê duyệt công văn")
					));
				}
				foreach( $usersDecide as $k => $uid ){
					if( !$this->Model->fetchRow("SELECT * FROM `users` WHERE `ID`='$uid' AND `is_deleted`='no'") ){
						continue;
					}
					if( isset($roles[$uid])){
						$roles[$uid]['is_proc'] = 'yes';
						$roles[$uid]['is_decide'] = 'yes';
					}else{
						$roles[$uid] = array(
							diploma_id	=> $diploma_id,
							user_id		=> $uid,
							is_decide	=> 'yes'
						);
					}
				}
				foreach( $roles as $role ){
					$this->Model->insert('diplomas_users',$role);
					$this->Model->insert('notices',array(
						created_by_id	=> $user_id,
						user_id			=> $role['user_id'],
						url				=> "#Diploma/Index/View?ID=$diploma_id",
						title			=> "Một công văn vừa được chỉnh sửa",
						date			=> mysql_date(),
						content			=> "Bạn vừa nhận được một công văn mới."
					));				
				}
				///////////////////////////////
				
				$files_selected = (array)$_POST['files'];
				$files = $this->Model->fetchAll("SELECT * FROM `diplomas_files` WHERE `diploma_id`='$diploma_id'");
				foreach( $files as $f ){
					if( !in_array( $f['ID'], $files_selected ) ){
						$this->Model->delete("diplomas_files","`ID`='{$f['ID']}'");
						@unlink("files/diploma/{$f['name']}");
					}
				}
				
				return $this->setJSON(array(
					redirect	=> "#Diploma"
				));
			}else{
				return $this->setJSON(array(
					message => error($data)
				));
			}			
		}
	}
	
	public function viewAction(){
		$id = getInt("ID",0);
		$user_id = get_user_id();
		
		
		$post = $this->Model->fetchRow("
			SELECT 	`a`.*,
					`b`.`title` as `subtype`,
					`c`.`username` as `updated_by_name`,`c`.`is_deleted` as `updated_is_deleted`,
					`d`.`username` as `decided_by_name`,`d`.`is_deleted` as `decided_is_deleted`
				FROM `diplomas` as `a`
					LEFT JOIN `diplomas_types` as `b` ON `b`.`ID`=`a`.`subtype`
					LEFT JOIN `users` as `c` ON `a`.`updated_by_id`=`c`.`ID`
					LEFT JOIN `users` as `d` ON `a`.`decided_by_id`=`d`.`ID`
				WHERE `a`.`ID`='$id'
		");
		$file = $this->Model->fetchAll("
			SELECT * 
			FROM `diplomas_files` 
			WHERE `diploma_id`='$id'
		");
		$userProc = $this->Model->fetchAll("
			SELECT `a`.*,`b`.*
			FROM `diplomas_users` as `a` 
				LEFT JOIN `users` as `b` ON `b`.`ID`=`a`.`user_id`
			WHERE `a`.`diploma_id`='$id' AND `a`.`is_proc`='yes'
		");
		$userDec = $this->Model->fetchAll("
			SELECT `a`.*,`b`.*
			FROM `diplomas_users` as `a` 
				LEFT JOIN `users` as `b` ON `b`.`ID`=`a`.`user_id`
			WHERE `a`.`diploma_id`='$id' AND `a`.`is_decide`='yes'
		");
		$comment = $this->Model->fetchAll("
			SELECT `a`.*,`b`.`username` as `created_by_name`,`b`.`is_deleted` as `created_is_deleted`,`b`.`photo` as `created_by_photo`
				FROM `diplomas_comments` as `a` 
					LEFT JOIN `users` as `b` ON `b`.`ID`=`a`.`created_by_id`
			WHERE `a`.`diploma_id`='$id'
		");
		
		$this->set(array(
			post 		=>	$post,
			files 		=>	$file,
			userProc	=>	$userProc,
			userDec		=>	$userDec,
			comment		=>  $comment,
			user_id		=>	$user_id
		));
	}
	
	public function deleteAction(){
		$id=getInt("ID",0);
		$user_id = get_user_id();
		
		$chk = $this->Model->fetchRow("SELECT * FROM `diplomas` WHERE `ID`='$id'");
		if($chk['created_by_id']!=$user_id){
			return $this->setJSON(array(
				alert => error("Bạn không có quyền xóa công văn này")
			));
		}
		$this->Model->delete("diplomas_files","`diploma_id`='$id'");
		$this->Model->delete("diplomas_comments","`diploma_id`='$id'");
		$this->Model->delete("diplomas_users","`diploma_id`='$id'");
		$this->Model->delete("diplomas"," `ID`='$id' ");
		return $this->setJSON(array(
			redirect	=> "#Diploma"
		));
	}
	
	protected function getDepartments( $id,$pus, $depth = 0, $selected='', $name='users' ){
		//Lay phong ban con
		$sub_departments 
			= $this->Model->fetchAll("SELECT `a`.*,IF(`b`.`ID`,`b`.`ID`,0) as `parentID` 
				FROM `departments` as `a` 
				LEFT JOIN `departments` as `b` ON 
				`a`.`parent_id`=`b`.`ID` HAVING `parentID`='$id'");
		$staffs = $this->Model->fetchAll("SELECT *,IF( '$selected' LIKE CONCAT('%,',`ID`,'%') , ' checked', '' ) as `checked`  FROM `users` WHERE `department_id`='$id'  AND `username` is not null");

		if(count($staffs) == 0 && count($sub_departments) == 0){
			return '';
		}	
			
		//class
		$class = $depth == 0 ? "tree-list tree-root":"tree-list";
			
		$list = "<div class='tree-list-outer'>
			<div class=\"$class\">";
		foreach( $sub_departments as $k => $sub ){
			$subs = $this->getDepartments( $sub['ID'], $depth+1, $selected );
			
			$cl = $subs !="" ? "" : " tree-link-empty";
			$cls = $subs !="" ? "" : " tree-folder-empty";
			
			//show child in depth
			$has_sub  = $this->Model->fetchOne("SELECT * FROM `departments` WHERE `parent_id`='{$sub['ID']}'");
			$is_close = $has_sub ? "" : " tree-section-close";
			
			$list .="<div class='tree-folder-outer'>
				<a class='tree-node' onclick='tree_collapse(this)'></a>
				<div class='tree-folder'>
					<a class='tree-title'><input type='checkbox' class='tree-check' onclick='tree_check_all(this)'/>{$sub['title']}</a>
					$subs
				</div></div>";
		}
		
		foreach( $staffs as $k => $staff ){
			$list .="<a class='tree-item-outer'>
				<span class='tree-item'><span class='tree-item-icon'></span>
				<input type='checkbox' name='$pus' value='{$staff['ID']}'{$staff['checked']}' class='tree-check'/>".$staff['username']."</span>
				</a>";					
		}
		$list .="</div></div>";
		return $list;
	}
	
	public function fileAction(){
		$file_id = get('ID',0);
		$user_id = get_user_id();
		$file = $this
			->Model->fetchRow("SELECT * FROM `diplomas_files` as `a` WHERE `a`.`ID`='$file_id' LIMIT 1");
		
		$file_path = "files/diploma/{$file['name']}";
		
		if( !file_exists( $file_path ) ){
			return $this->setError('404');
		}	
		if( get('type') == 'icon' ){	
			$img_types = array('image/gif','image/png','image/x-png','image/jpeg','image/x-jpeg','image/jpg');
			if( in_array($file['type'],$img_types) ){
				loadClass('PHPThumb');
				$thumb = PhpThumbFactory::create($file_path);
				$thumb->resize(60, 60);
				$thumb->show();
			}else{
				@readfile("files/static/icon.gif");	
			}		
		}else if( get('type') == 'view' ){	
			$img_types = array('image/gif','image/png','image/x-png','image/jpeg','image/x-jpeg','image/jpg');
			if( in_array($file['type'],$img_types) ){
				loadClass('PHPThumb');
				$thumb = PhpThumbFactory::create($file_path);
				$thumb->show();
			}else{
				@readfile("files/static/icon.gif");	
			}		
		}else{
			@header("Content-Disposition: attachment; filename={$file['name']}");
			@readfile($file_path);
		}
	}
	
	public function deletecommentAction(){
		$id = getInt("ID",0);
		$diploma_id = get('diploma_id');
		$this->Model->delete("diplomas_comments","`ID`='$id'");
		return $this->setJSON(array(
			redirect => "#Diploma/Index/View?ID=".$diploma_id
		));
	}
	
	public function editcommentAction(){
		$this->removeLayout();
		$this->setView('IndexComment');
		$id = getInt('ID',0);
		$diploma_id = getInt('diploma_id',0);
		$cr = $this->Model->fetchRow("SELECT * FROM `diplomas_comments` WHERE `ID`='$id'");
		
		$this->set(array(
			crComment 	=> 	$cr,
			diploma_id	=>	$diploma_id
		));
		if(isPost()){
			$diploma_id = $_POST['diploma_id'];
			$id 		= $_POST['ID'];
			
			loadClass("ZData");
			$form = new ZData();
			$form->addFields( $this->CommentFields($cr));
			$data = $form->getData();
			if( !is_array($data) ){
				return $this->setJSON(array(
					message => error($data)
				));
			}
			$this->Model->update("diplomas_comments",$data," `ID`='$id' ");
			return $this->setJSON(array(
				redirect => "#Diploma/Index/View?ID=".$diploma_id
			));
		}
	}
	
	public function decideAction(){
		$id = getInt("ID",0);
		$user_id = get_user_id();
		$post = $this->Model->fetchRow("SELECT * FROM `diplomas` WHERE `ID`='$id'");
		$chk = $this->Model->fetchAll("SELECT * FROM `diplomas_users` WHERE `diploma_id`='$id' AND `is_decide`='yes'");
		$a=array();
		foreach($chk as $v){
			$a[]=$v['user_id'];
		}
		if(!in_array($user_id,$a)){
			return $this->setJSON(array(
				alert => "Bạn không có quyền phê duyệt hoặc bỏ phê duyệt công văn này."
			));
		}
		if($post['decided']=='yes'){
			$f1='no';
			$f2=NULL;
			$f3=NULL;
		}else{
			$f1='yes';
			$f2=$user_id;
			$f3=mysql_date();
		}
		
		$this->Model->update("diplomas",array(
			'decided'		=>$f1,
			'decided_by_id'	=>$f2,
			'date_decided'	=>$f3
		),"`ID`='$id'");
		$a = array();
		$ar = $this->Model->fetchAll("SELECT * FROM `diplomas_users` WHERE `diploma_id`='$id'");
		foreach($ar as $k){
			$a[]=$k['user_id'];
		}
		foreach($a as $v){
			$this->Model->insert('notices',array(
						created_by_id	=> $user_id,
						user_id			=> $v,
						url				=> "#Diploma/Index/View?ID=$id",
						title			=> "Một công văn mới được chuyển trạng thái",
						date			=> mysql_date(),
						content			=> "..."
			));
		}
		return $this->setJSON(array(
			redirect => "#Diploma/Index/View?ID=".$id
		));
		
	}
	public function forwardAction(){
		$this->removeLayout();
		$diploma_id = getInt('ID',0);
		
		$this->set(array(
			diploma_id =>$diploma_id
		));
		if(isPost()){
			$diploma_id = $_POST['diploma_id'];
			$a = array();
			$usersProc = (array)$_POST['usersProc'];				
			$crProc = $this->Model->fetchAll("SELECT * FROM `diplomas_users` WHERE `diploma_id`='$diploma_id' AND `is_proc`='yes'");
			foreach($crProc as $k){
				$a[]=$k['user_id'];
			}
			
			$arr = array_unique(array_merge($a,$usersProc));
			$string_join = join(",",$arr);
			
			$this->Model->update("diplomas_users",array("is_proc"=>NULL),"`user_id` in ( $string_join )");
			foreach( $arr as $k => $uid ){
				if( !$this->Model->fetchRow("SELECT * FROM `users` WHERE `ID`='$uid' AND `is_deleted`='no'") ){
					continue;
				}	
				$arr = array(
					diploma_id	 => $diploma_id,
					user_id		 => $uid,
					is_proc => 'yes'
				);
				$this->Model->insert('diplomas_users',$arr);				
			}
			return $this->setJSON(array(
				redirect => "#Diploma/Index/View?ID=".$diploma_id
			));
		}
		
	}
	public function commentAction(){
		$this->removeLayout();
		$user_id = get_user_id();
		$diploma_id = getInt("ID",0);
		
		
		
		$this->set(array(
			diploma_id => $diploma_id
		));
		if(isPost()){
			$diploma_id = $_POST['diploma_id'];
			loadClass("ZData");
			$form = new ZData();
			$form->addFields( $this->CommentFields());
			$data = $form->getData();
			if( !is_array($data) ){
				return $this->setJSON(array(
					message => error($data)
				));
			}	
			$data['diploma_id'] = $diploma_id;
			$this->Model->insert("diplomas_comments",$data);
			
			return $this->setJSON(array(
				redirect => "#Diploma/Index/View?ID=".$diploma_id
			));
		}
	}
}
?>